*******************************************************************************
* Loop over subgroups
*******************************************************************************

foreach subgroup in All Democrat Republican {
	
*******************************************************************************
* Load data
*******************************************************************************

use dataset_gss, clear

*******************************************************************************
* Keep the subgroup
*******************************************************************************

keep if subgroup == "`subgroup'"

*******************************************************************************
* Make lags
*******************************************************************************

local lags
levelsof year if year < 2014, local(levels)
foreach i of local levels {
	egen nat`i'm = mean(nat) if year == `i', by(target)
	egen nat`i' = mean(nat`i'm), by(target)
	drop nat`i'm
	local lags `lags' nat`i'
}

*******************************************************************************
* Set panel and get the treated ID
*******************************************************************************

encode target, gen(id)
xtset id year
su id if target == "crimy"

*******************************************************************************
* Synth
*******************************************************************************

synth nat `lags', trunit(`r(mean)') trperiod(2014) keep(tmp, replace)

use tmp, clear
erase tmp.dta

*******************************************************************************
* Get table of weights
*******************************************************************************

keep if !mi(_W_)
drop _Y_* _time
rename _W_ Weight_`subgroup'
save tmp_`subgroup', replace

*******************************************************************************
* Close subgroup loop
*******************************************************************************

}

*******************************************************************************
* Combine tables
*******************************************************************************

use tmp_All, clear
merge 1:1 _Co_Number using tmp_Democrat
drop _m
merge 1:1 _Co_Number using tmp_Republican
drop _m

erase tmp_All.dta
erase tmp_Democrat.dta
erase tmp_Republican.dta

*******************************************************************************
* Format table
*******************************************************************************

decode _Co_, gen(target)
replace target = subinstr(target, "_", " ", 1)
drop _Co_
order target

replace target = "Foreign aid" if target == "aid"
replace target = "Foreign aid alt" if target == "aidy"
replace target = "Military" if target == "arms"
replace target = "Military alt" if target == "armsy"
replace target = "Cities" if target == "city"
replace target = "Cities alt" if target == "cityy"
replace target = "Crime" if target == "crime"
replace target = "Drugs" if target == "drug"
replace target = "Drugs alt" if target == "drugy"
replace target = "Education" if target == "educ"
replace target = "Education alt" if target == "educy"
replace target = "Environment" if target == "envir"
replace target = "Environment alt" if target == "enviy"
replace target = "Welfare" if target == "fare"
replace target = "Welfare alt" if target == "farey"
replace target = "Health" if target == "heal"
replace target = "Health alt" if target == "healy"
replace target = "Transportation" if target == "mass"
replace target = "Parks and rec" if target == "park"
replace target = "Blacks" if target == "race"
replace target = "Blacks alt" if target == "racey"
replace target = "Roads" if target == "road"
replace target = "Social Security" if target == "soc"
replace target = "Space" if target == "spac"
replace target = "Space alt" if target == "spacy"

sort target

*******************************************************************************
* Bold non-zero entries
*******************************************************************************

gen any_nonzero = Weight_A > 0 | Weight_D > 0 | Weight_R > 0
tostring Weight*, replace force format(%9.0g)
foreach x in target Weight_A Weight_D Weight_R {
	replace `x' = "\bf{" + `x' + "}" if any_nonzero == 1 & `x' != "0"
	replace `x' = "" if `x' == "0"
}
drop any_nonzero 

*******************************************************************************
* Output table
*******************************************************************************

#delimit;

listtex 
	using "table_gss_sc.tex"
	,
	replace
	type rstyle(tabular)
	head(
		\begin{tabular*}
		{\hsize}{@{\hskip\tabcolsep\extracolsep\fill}l*{4}{l}}
		\toprule 
		Series & Weight All & Weight Dem & Weight Rep
		\\
		\midrule
	)
	foot("\bottomrule\end{tabular*}")
	;
	
#delimit cr

*******************************************************************************
* End
*******************************************************************************
